package com.cqut.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cqut.dao.RoleRigthMapper;
import com.cqut.dto.LoginInfoDTO;
import com.cqut.dto.RoleRigthDTO;
import com.cqut.model.Role;
import com.cqut.model.RoleRigth;
import com.cqut.service.IRoleRigthService;
import com.cqut.util.SqlUtil;
import com.cqut.util.SystemConstants;

@Service
public class RoleRigthService implements IRoleRigthService {
    @Resource(name="roleRigthMapper")
    private RoleRigthMapper roleRigthMapper;

    @Override
    public boolean save(RoleRigth roleRigth) {
        return roleRigthMapper.insert(roleRigth) > 0;
    }

    @Override
    public boolean update(RoleRigth roleRigth) {
        return roleRigthMapper.updateByPrimaryKeySelective(roleRigth) >= 0;
    }

    @Override
    public boolean delete(String roleId) {
        return roleRigthMapper.deleteByPrimaryKey(roleId) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return roleRigthMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public RoleRigth findModel(String roleRightId) {
        return roleRigthMapper.selectByPrimaryKey(roleRightId);
    }

    @Override
    public RoleRigth findModel(String roleRightId, String[] parameters) {
        return roleRigthMapper.findModel(roleRightId, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String roleRightId) {
        String[][] parameters = {{"role_right_id", "roleRightId"}, {"menu_id", "menuId"}, {"remark", "remark"}};
        return this.findMap(roleRightId, parameters);
    }

    public Map<String, Object> findMap(String roleRightId, String[][] parameters) {
        return roleRigthMapper.findMap(roleRightId, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<RoleRigth> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<RoleRigth> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<RoleRigth> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return roleRigthMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"role_right_id", "roleRightId"}, {"menu_id", "menuId"}, {"remark", "remark"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"role_right_id", "roleRightId"}, {"menu_id", "menuId"}, {"remark", "remark"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return roleRigthMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return roleRigthMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }

    /**
     * 获得最大的roleRightId
     * **/
    public int getMaxRoleRightId(){
		Map<String, Object> maxRoleRightId = roleRigthMapper.findMaxCodeByRoleRightId();
		
		String temp = maxRoleRightId.values().toString();
		
		int max = Integer.parseInt(temp.substring(1,temp.length()-1));
		max++;
		return max;
	}
	
    
	public List<Map<String, Object>> findPermById(String roleId) {

		List<Map<String, Object>> modules = roleRigthMapper.findModuleIdByOwner(roleId);
		
		return convert(modules);
	}

	/**
	 * 将查询得到的数据转换成一定的格式
	 * */
	private List<Map<String, Object>> convert(List<Map<String, Object>> list) {
		List<Map<String, Object>> test = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		
		if (list != null) {
			int initialLength = ((String) list.get(0).get("menu_id")).length();
			int i = 0;
			for (i = 0; i < list.size(); i++) {
				String parent = (String) list.get(i).get("menu_id");
				Map<String, Object> map = new HashMap<String, Object>();
				List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();

				if (parent.length() == initialLength) {
					map = list.get(i);
					for (int j = i + 1; j < list.size(); j++) {
						if (((String) list.get(j).get("menu_id")).length() == initialLength) {
							i = j - 1;
							break;
						}
						children.add(list.get(j));
					}

					map.put("children", children);
					test.add(map);
					
					result.add(map);
				}

			}
		}
		
		for(int j=0;j<test.size()-1;j++){
			for(int l=0;l<test.size()-1-j;l++){
				Map<String, Object> map = test.get(l);
				int line=Integer.parseInt(map.get("order_no").toString());
				
				Map<String, Object> secondMap = test.get(l+1);
				int secondLine=Integer.parseInt(secondMap.get("order_no").toString());
					
				if (line > secondLine){
					test.set(l+1, map);
					test.set(l, secondMap);
                 }
			}
		}
		
		for (int j = 0; j < test.size(); j++) {
			Map<String, Object> map = test.get(j);
			int line=Integer.parseInt(map.get("order_no").toString());
			
			List<Map<String, Object>> temp=new ArrayList<Map<String, Object>>();
			temp=(List<Map<String, Object>>) map.get("children");
			
			//冒泡排序
			for (int i = 0; i < temp.size()-1; i++)
            {
                for (int k = 0; k < temp.size()- i - 1; k++)
                {
                	Map<String, Object> map1=temp.get(k);
    				int line1=Integer.parseInt(map1.get("order_no").toString());
                	
                	Map<String, Object> map2=temp.get(k+1);
                	int line2=Integer.parseInt(map2.get("order_no").toString());
                	
                    if (line1 > line2){ 
                       temp.set(k+1, map1);
                       temp.set(k, map2);
                    }
                }
            }
			
		}
		
		return test;
	}
	
	
	/**
	 * 根据roleId获得permissionIds
	 * **/
	public List<Map<String, Object>> findPermissionIds(String roleId) {
		return roleRigthMapper.findPermissionIds(roleId);
	}

	
	
}

    
    

